<!--  -->
<template>
  <div class="userlist">
    <el-card class="box-card">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-card>
    <el-card class="box-card">
      <el-table :data="userListDatas.data" stripe border style="width: 100%">
        <el-table-column
          prop="user_id"
          label="序号"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="user_name"
          label="姓名"
          width="180"
        ></el-table-column>
        <el-table-column label="用户头像">
          <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.imgsrc"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column
          prop="phone"
          label="手机号"
          width="180"
        ></el-table-column>
        <el-table-column prop="address" label="地址"></el-table-column>
        <el-table-column label="是否可用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_show"
              active-text="开启"
              :active-value="1"
              active-color="#13ce66"
              inactive-text="关闭"
              inactive-color="#ff4949"
              @change="changeshow(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="210">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="info" size="small"
              >查看</el-button
            >
            <el-button type="primary" size="small" @click="editFn(scope.row)"
              >编辑</el-button
            >
            <el-button type="danger" size="small" @click="deleFn(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        :page-size="5"
        :total="userListDatas.count"
        @current-change="changePage"
      ></el-pagination>
    </el-card>

    <!-- 修改用户 -->
    <el-dialog title="修改用户" :visible.sync="flag">
      <el-form :model="editlist" :rules="rules" ref="ref">
        <el-form-item prop="user_name">
          <el-input v-model="editlist.user_name" placeholder="邮箱"></el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input v-model="editlist.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="用户头像">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:3008/api/user/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="flag = false">取 消</el-button>
        <el-button type="primary" @click="editsuccessFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { EditUser, EditIsShow, DeleUser } from "@/api/index";
import tools from "@/utils/index";
// console.log(EditUser);
// console.log(EditIsShow);
// console.log(DeleUser);
export default {
  //import引入的组件需要注入到components对象中才能使用
  components: {},
  data() {
    var rex = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入姓名或手机号"));
      } else {
        if (!(tools.rexPhone.test(value) || tools.rexname.test(value))) {
          callback(new Error("请输入正确姓名或手机号"));
        }
        callback();
      }
    };
    //这里存放数据
    return {
      page: 1,
      size: 5,
      editlist: {},
      flag: false,
      rules: {
        user_name: [{ validator: rex, trigger: "blur" }],
        phone: [{ validator: rex, trigger: "blur" }],
      },
      imageUrl: "",
    };
  },
  methods: {
    getuserlist(params) {
      this.$store.dispatch("actchangeuserListDatas", params);
    },
    handleClick(rowData) {
      console.log(rowData);
    },
    changePage(page) {
      this.page = page;
      this.getuserlist({
        page: this.page,
        size: this.size,
      });
    },
    // 删除用户
    deleFn(rowData) {
      DeleUser(rowData);
      this.getuserlist({
        page: this.page,
        size: this.size,
      });
    },
    // 编辑按钮
    editFn(rowData) {
      this.editlist = rowData;
      this.flag = !this.flag;
    },
    // 编辑成功
    editsuccessFn() {
      this.$refs.ref.validate((valid) => {
        if (valid) {
          let obj = {
            user_name: this.editlist.user_name,
            phone: this.editlist.phone,
            user_id: this.editlist.user_id,
            imgsrc: this.imageUrl,
          };
          EditUser(obj).then((res) => {
            // console.log(res);
            if (res.status == 200) {
              this.flag = false;
              this.getuserlist({
                page: this.page,
                size: this.size,
              });
            }
          });
        } else {
          console.log("错误");
          return false;
        }
      });
    },
    // 改变is_show
    changeshow(rowData) {
      let num = Number(rowData.is_show);
      EditIsShow({ is_show: num, user_id: rowData.user_id }).then((res) => {
        if (res.status == 200) {
          console.log("修改成功");
        }
      });
    },
    handleAvatarSuccess(res) {
      this.imageUrl = "http://localhost:3008/" + res.data.name;
    },
    beforeAvatarUpload() {},
  },
  computed: {
    userListDatas() {
      return this.$store.state.userListDatas;
    },
  },
  mounted() {
    this.getuserlist({
      page: this.page,
      size: this.size,
    });
  },
};
</script>
<style lang="less">
.el-row {
  .el-breadcrumb {
    margin-bottom: 20px;
  }
}
.el-pagination {
  margin-top: 20px;
}
.el-switch__core {
  width: 60px !important;
}
.el-switch__label {
  display: none;
}
.el-switch__label--right {
  position: absolute;
}
.el-switch__label.is-active {
  display: block;
  color: #ffffff;
  font-size: 12px;
}
.el-switch__label--left {
  position: absolute;
  left: 20px;
  z-index: 99;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 118px;
  height: 118px;
  line-height: 118px;
  text-align: center;
}
.avatar {
  width: 118px;
  height: 118px;
  display: block;
}
</style>